import { routerRedux } from 'dva/router';
import { fakeRegister } from '../services/api';
import { setAuthority } from '../utils/authority';

export default {
  namespace: 'register',

  state: {
    status: undefined,
  },

  effects: {
    *submit(_, { call, put }) {
      const response = yield call(fakeRegister);
      localStorage.setItem("menu",JSON.stringify(response.permissions.menu));
      yield put({
        type: 'registerHandle',
        payload: {
          status: true,
          currentAuthority: response.permissions.role,
        },
      });
      // Register successfully
      if (response.message === 'ok') {
        yield put(routerRedux.push('/user/register-result'));
      }
    },
  },

  reducers: {
    registerHandle(state, { payload }) {
      setAuthority(payload.currentAuthority);
      return {
        ...state,
        status: payload.status,
      };
    },
  },
};
